package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.edu.model.entity.LearnTask;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.AnimaQuery;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @AUTHOR soft
 * @DATE 2018/10/7 15:41
 * @DESCRIBE
 */
@Bean
public class LearnTaskService implements Service<LearnTask> {

    /**
     * 更新任务学习状态
     * @param ltid 任务学习id
     * @param time 学习时间
     * @param rate 学习比例
     */
    public boolean updateTime(Integer ltid, long time, int rate) {
        LearnTask learnTask = byId(ltid);
        if (learnTask.getRate() < rate) {
            return Anima.update().from(LearnTask.class)
                    .set(LearnTask::getLearnTime, time)
                    .set(LearnTask::getRate, rate)
                    .where(LearnTask::getId, ltid)
                    .execute() > 0;
        }
        return true;
    }

    public boolean isJoin(int uid, int tid) {
        return Anima.select().from(LearnTask.class)
                .where(LearnTask::getTaskId, tid)
                .where(LearnTask::getUserId, uid)
                .count() > 0;
    }

    @Override
    public LearnTask add(LearnTask learnTask) {
        Integer integer = learnTask.save().asInt();
        if (integer > 0) {
            learnTask.setId(integer);
            return learnTask;
        }
        return null;
    }

    @Override // 不使用
    public LearnTask delete(Object pk) {
        LearnTask learnTask = byId(pk);
        if (learnTask != null) {
            learnTask.delete();
            return learnTask;
        }
        return null;
    }

    @Override
    public boolean change(LearnTask learnTask) {
        if (learnTask!= null && learnTask.getId() > 0) {
            return learnTask.update() > 0;
        }
        return false;
    }

    @Override
    public LearnTask byId(Object pk) {
        return Anima.select().from(LearnTask.class).byId(pk);
    }

    /**
     * 通过用户id和任务id获取 任务学习情况
     * @param uid    用户id
     * @param taskId 任务id
     */
    public LearnTask byUidAndTaskId(int uid, int taskId) {
        return Anima.select().from(LearnTask.class)
                .where(LearnTask::getUserId, uid)
                .where(LearnTask::getTaskId, taskId)
                .one();
    }

    /**
     * 统计某学生在某课程的任务学习数 完成的或全部
     * @param kid 课程id
     * @param uid 用户id
     * @param finish 完成或全部
     */
    public long count(int kid, int uid, boolean finish) {
        AnimaQuery<LearnTask> where = Anima.select().from(LearnTask.class)
                .where(LearnTask::getCourseId, kid)
                .where(LearnTask::getUserId, uid);
        if (finish) {
            where.where(LearnTask::getRate).gte(100);
        }
        return where.count();
    }

    /**
     * 获取当前用户在该课程下所有已完成的任务id列表
     * @param kid 课程id
     * @param uid 用户id
     */
    public List<Integer> getFinishIds(int kid, int uid) {
        return Anima.select().from(LearnTask.class)
                .where(LearnTask::getUserId, uid)
                .where(LearnTask::getCourseId, kid)
                .where(LearnTask::getRate).gte(100)
                .all().stream().map(LearnTask::getId).collect(Collectors.toList());
    }

    public Long countByUid(Integer userId) {
        return Anima.select().from(LearnTask.class)
                .where(LearnTask::getUserId, userId)
                .count();
    }

    public int deleteByKid(Integer kid) {
        return Anima.delete().from(LearnTask.class).where(LearnTask::getCourseId, kid).execute();
    }

    /**
     * 根据用户id和课程id 清除学习记录
     * @param kid 课程id
     * @param uid 用户id
     */
    public int unjoin(Integer kid, Integer uid) {
        return Anima.delete().from(LearnTask.class)
                .where(LearnTask::getCourseId, kid)
                .where(LearnTask::getUserId, uid)
                .execute();
    }
}
